﻿var agencyVerify = {};
agencyVerify.submitFun = [];
//非负整数文本框
agencyVerify.nonnegativeIntInput = function (selector, dynamic) {
    var fun = function (e) {
        var temvaule = e.target.value;
        var point = agencyVerify.getTxt1CursorPosition(e.target);
        var patrn = /0|^\d+(\.\d+)?/;
        if (temvaule.match(patrn)) {
            e.target.value = e.target.value.match(patrn)[0];
            if (e.type == "keyup") {
                agencyVerify.setCursor(e.target, point);
            }
        } else {
            e.target.value = "";
        }

    }
    if (dynamic) {
        $(selector).live("keyup", fun);
        $(selector).live("blur", fun);
    } else {
        $(selector).bind("keyup", fun);
        $(selector).bind("blur", fun);
    }
};
//0-9数字验证
agencyVerify.greaterThanEqualZero = function (selector, dynamic) {
    var fun = function (e) {
        var temvaule = e.target.value;
        var point = agencyVerify.getTxt1CursorPosition(e.target);
        var patrn = /0(\d+)?|^\d+(\d+)?/;
        if (temvaule.match(patrn)) {
            e.target.value = e.target.value.match(patrn)[0];
            if (e.type == "keyup") {
                agencyVerify.setCursor(e.target, point);
            }
        } else {
            e.target.value = "";
        }
    }
    if (dynamic) {
        $(selector).live("keyup", fun);
        $(selector).live("blur", fun);
    } else {
        $(selector).bind("keyup", fun);
        $(selector).bind("blur", fun);
    }
};

//1-9数字验证
agencyVerify.greaterThanZero = function (selector, dynamic) {
    var fun = function (e) {
        var temvaule = e.target.value;
        var patrn = /^[1-9]+(\d+)?/;
        var point = agencyVerify.getTxt1CursorPosition(e.target);
        if (temvaule.match(patrn)) {
            e.target.value = e.target.value.match(patrn)[0];
            if (e.type == "keyup") {
                agencyVerify.setCursor(e.target, point);
            }
        } else {
            e.target.value = "";
        }
    }
    if (dynamic) {
        $(selector).live("keyup", fun);
        $(selector).live("blur", fun);
    } else {
        $(selector).bind("keyup", fun);
        $(selector).bind("blur", fun);
    }
};

//正负整数文本框
agencyVerify.zhengFuIntInput = function (selector, dynamic) {
    var fun = function (e) {
        var temvaule = e.target.value;
        var patrn = /([-,0]|\d+)(\d+)?/;
        var point = agencyVerify.getTxt1CursorPosition(e.target);
        if (temvaule.match(patrn)) {
            e.target.value = e.target.value.match(patrn)[0];
            if (e.type == "keyup") {
                agencyVerify.setCursor(e.target, point);
            }
        }
        else {
            e.target.value = "";
        }
    }
    if (dynamic) {
        $(selector).live("keyup", fun);
        $(selector).live("blur", fun);
    }
    else {
        $(selector).bind("keyup", fun);
        $(selector).bind("blur", fun);
    }
}

//大于等于零的float
agencyVerify.nonnegativeInput = function (selector, dynamic) {
    var fun = function (e) {
        var temvaule = e.target.value;
        var patrn = /^([1-9]\d*\.\d*|0\.\d*|[1-9]\d*|0)/;
        var point = agencyVerify.getTxt1CursorPosition(e.target);
        if (temvaule.match(patrn)) {
            e.target.value = e.target.value.match(patrn)[0];
            if (e.type == "keyup") {
                agencyVerify.setCursor(e.target, point);
            }
        } else {
            e.target.value = "";
        }
    }
    if (dynamic) {
        $(selector).live("keyup", fun);
        $(selector).live("blur", fun);
    } else {

        $(selector).bind("keyup", fun);
        $(selector).bind("blur", fun);
    }
};

//检查ip地址是否正确
agencyVerify.checkIpAdressInput = function (selector, dynamic) {
    var fun = function (e) {
        var temvaule = e.target.value;
        var patrn = /^((2[0-4]\d|25[0-5]|[01]?\d\d?)\.){3}(2[0-4]\d|25[0-5]|[01]?\d\d?)$/;
        var point = agencyVerify.getTxt1CursorPosition(e.target);
        if (temvaule.match(patrn)) {
            e.target.value = e.target.value.match(patrn)[0];
            if (e.type == "keyup") {
                agencyVerify.setCursor(e.target, point);
            }
        } else {
            e.target.value = "";
        }
    }
    if (dynamic) {
        $(selector).live("keyup", fun);
        $(selector).live("blur", fun);
    } else {

        $(selector).bind("keyup", fun);
        $(selector).bind("blur", fun);
    }
};


//大于零文本框
agencyVerify.greaterThanZeroInput = function (selector, dynamic) {
    var fun = function (e) {
        var temvaule = e.target.value;
        var patrn = /^([1-9]\d*\.\d*|0\.\d*|[1-9]\d*)/;
        var point = agencyVerify.getTxt1CursorPosition(e.target);
        if (temvaule.match(patrn)) {
            e.target.value = e.target.value.match(patrn)[0];
            if (e.type == "keyup") {
                agencyVerify.setCursor(e.target, point);
            }
        } else {
            e.target.value = "";
        }
    }
    if (dynamic) {
        $(selector).live("keyup", fun);
        $(selector).live("blur", fun);
    } else {

        $(selector).bind("keyup", fun);
        $(selector).bind("blur", fun);
    }
};

//两个数的大小比较
agencyVerify.bigOrSmallCompare = function (ele1Id, ele2Id) {
    var data1 = parseFloat($('#' + ele1Id).val());
    var data2 = parseFloat($('#' + ele2Id).val());
    if (data1 && data2) {
        if (data1 > data2) {
            $('#' + ele1Id).val(data2);
            $('#' + ele2Id).val(data1);
        }
    }
};

//两个数比较大小有提示,可与非空验证合用，但不适合跟其它提示弄验证合用
agencyVerify.bigOrSmallCompareAlert = function (form, selector1, selector2, msg, dynamic) {
    var formSubmit = function () {
        var temarry = $(selector1);
        var temarry2 = $(selector2);
        var wrongspan = temarry2.next();
        if (temarry.val() == "" || temarry2.val() == "" || temarry.val() == "-1" || temarry2.val() == "-1") {
            if (wrongspan.attr("class") == "wrong" && wrongspan.text() == msg) {
                wrongspan.remove();
                temarry2.removeClass("checkError");
                //清空掉错误后，要再次触发blur事件看看别的实验是否成功
                temarry2.blur();
                return true;
            }
            else {
                return true;
            }

        }
        if (Number(temarry2.val()) < Number(temarry.val())) {
            //判断第二个元素后面有没有跟错误信息
            if (wrongspan.attr("class") != "wrong") {
                //temarry2.after("<span class='wrong'>" + msg + "</span>");
                temarry2.after(agencyVerify.greatErrorHtml(temarry2, msg));
                temarry2.addClass("checkError");
                return false;
            }
            else {
                return false;
            }

        }
        else {
            if (wrongspan.attr("class") == "wrong" && wrongspan.text() == msg) {
                wrongspan.remove();
                temarry2.removeClass("checkError");
            }
            return true;
        }
    }
    if (dynamic) {
        $(form).on("blur", selector2, formSubmit);
        $(form).on("blur", selector1, formSubmit);
    }
    else {
        temarry2.bind("blur", formSubmit);
        temarry.bind("blur", formSubmit);
    }
    $(form).bind("submit", formSubmit);
}

//年龄检测
agencyVerify.checkAge = function (selector, dynamic) {
    var fun = function (e) {
        var temvaule = e.target.value;
        var patrn = /^((100)|[1-9]\d{0,1})/;
        if (temvaule.match(patrn)) {
            e.target.value = e.target.value.match(patrn)[0];
        } else {
            e.target.value = "";
        }
    }
    if (dynamic) {
        $(selector).live("keyup", fun);
        $(selector).live("blur", fun);
    } else {

        $(selector).bind("keyup", fun);
        $(selector).bind("blur", fun);
    }
};

//不能出现重复的手机号
agencyVerify.NoSame = function (form, selector, method) {
    if (!method) {
        method = 'change';
    }
    var formSubmit = function () {
        var temresult = true;
        var temarry = $(selector);
        var temarry2 = temarry;
        for (var i = 0; i < temarry.length; i++) {
            var temel = $(temarry[i]);
            if (noSametem(temel, temarry)) {
                var temwrongspan = temel.next();
                if (temwrongspan.text() == "手机号有重复") {
                    temwrongspan.remove();
                    temel.removeClass("checkError");
                }
            }
            else {
                temresult = false;
            }

        }
        return temresult;
    }
    var noSametem = function (el, els) {
        var temsametag = true;
        var wrongspan = $(el).next();
        for (var k = 0; k < els.length; k++) {
            if (el[0] == els[k]) {
                continue;
            }
            if (el.val().length != 11) {
                continue;
            }
            if (el.val() == els[k].value) {
                temsametag = false;
                if (wrongspan.text() != "手机号有重复") {
                    el.addClass("checkError");
                    el.after(agencyVerify.greatErrorHtml(el, '手机号有重复'));
                }
                break;
            }
        }
        return temsametag;
    }
    //$(selector).live(method, formSubmit);
    $(form).on(method, selector, formSubmit);
    $(form).bind("submit", formSubmit);
};

//不能出现重复的座机号
agencyVerify.NoSameTel = function (form, selector, method, msg) {
    if (!method) {
        method = 'change';
    }
    var formSubmit = function () {
        var temresult = true;
        var temarry = $(selector);
        var temarry2 = temarry;
        for (var i = 0; i < temarry.length; i++) {
            var temel = $(temarry[i]);
            if (noSametemTel(temel, temarry)) {
                var temwrongspan = temel.next();
                if (temwrongspan.text() == msg) {
                    temwrongspan.remove();
                    temel.removeClass("checkError");
                }
            }
            else {
                temresult = false;
            }

        }
        return temresult;
    }
    var noSametemTel = function (el, els) {
        var temsametag = true;
        var wrongspan = $(el).next();
        for (var k = 0; k < els.length; k++) {
            if (el[0] == els[k]) {
                continue;
            }
            if (el.val().length == 0) {
                continue;
            }
            if (el.val() == els[k].value) {
                temsametag = false;
                if (wrongspan.text() != msg) {
                    el.addClass("checkError");
                    el.after(agencyVerify.greatErrorHtml(el, msg));
                }
                break;
            }
        }
        return temsametag;
    }
    //$(selector).live(method, formSubmit);
    $(form).on(method, selector, formSubmit);
    $(form).bind("submit", formSubmit);
};

//动态添加时不能出现重复的名称
agencyVerify.NoInputSame = function (form, selector, method, msg) {
    if (!method) {
        method = 'change';
    }
    var formSubmit = function () {
        var temarry = $(selector);
        var temarry2 = temarry;
        for (var i = 0; i < temarry.length; i++) {
            var temval = temarry[i].value;
            if (temval) {
                for (var k = i + 1; k < temarry2.length; k++) {
                    var wrongspan = $(temarry2[k]).next();
                    if (temval == temarry2[k].value) {
                        if (wrongspan.attr("class") != "wrong") {
                            $(temarry2[k]).addClass("checkError");
                            //$(temarry2[k]).after("<span class='wrong'>" + msg + "</span>");
                            $(temarry2[k]).after(agencyVerify.greatErrorHtml($(temarry2[k]), msg));
                            return false;
                        }
                        else {
                            return false;
                        }
                    }
                    else if (wrongspan.attr("class") == "wrong" && wrongspan.text() == msg) {
                        wrongspan.remove();
                        $(temarry2[k]).removeClass("checkError");
                        $(temarry2[k]).change();
                    }
                }
            }
        }
        return true;
    }
    //$(selector).live(method, formSubmit);
    $(form).on(method, selector, formSubmit);
    $(form).bind("submit", formSubmit);
};


//给系统设置里填充颜色
agencyVerify.SetColor = function (form, selector, method) {
    if (!method) {
        method = 'change';
    }
    var formSubmit = function () {
        var temarry = $(selector);
        for (var i = 0; i < temarry.length; i++) {
            var temval = temarry[i].value;
            if (temval) {
                $(temarry[i]).prev().css("background-color", $(temarry[i]).val());
            }
        }
    }
    $(form).on(method, selector, formSubmit);
    $(form).bind("submit", formSubmit);
};


//异步处理不能出现重复的名称（如员工名称，角色名称等等）
agencyVerify.NoInputNameSame = function (form, selector, method, msg, url, myEmployeeNo) {
    if (!method) {
        method = 'change';
    }
    var formSubmit = function () {
        var temarry = $(selector);
        var wrongspan = temarry.next();
        var reqValue = temarry.val();
        var reqPropertyKeyId = temarry.attr("propertykeykeyid");
        if (reqPropertyKeyId != null && reqPropertyKeyId != "") {
            reqValue = reqValue + "~" + reqPropertyKeyId;
        }
        var reqData = {

            "value": reqValue

        };
        if (reqValue.length > 0) {
            $.AjaxAgency(url, reqData, function (data) {
                if (data == "True") {
                    //判断，如果编辑时名称是本身即不需要验证
                    if (myEmployeeNo != reqValue) {
                        if (wrongspan.attr("class") != "wrong") {
                            temarry.addClass("checkError");
                            //temarry.after("<span class='wrong'>" + msg + "</span>");
                            temarry.after(agencyVerify.greatErrorHtml(temarry, msg));
                            return false;
                        }
                    }
                    else {
                        if (wrongspan.attr("class") == "wrong" && wrongspan.text() == msg) {
                            wrongspan.remove();
                            temarry.removeClass("checkError");
                            temarry.change();
                        }
                    }
                }
                else if (data == "False") {
                    if (wrongspan.attr("class") == "wrong" && wrongspan.text() == msg) {
                        wrongspan.remove();
                        temarry.removeClass("checkError");
                        temarry.change();
                    }
                }
                else {
                    $.OKDialogAgency('<div class="tc">' + data + '</div>');
                }
            });
        }
        else {
            if (wrongspan.attr("class") == "wrong" && wrongspan.text() == msg) {
                wrongspan.remove();
                temarry.removeClass("checkError");
                temarry.change();
            }
        }
        return true;
    }
    // $(selector).unbind(method);
    $(selector).bind(method, formSubmit);
    //$(form).bind("submit", formSubmit);
};


//手机号
agencyVerify.mobileInput = function (form, selector, dynamic, event) {
    agencyVerify.regexCheck(form, selector, /^[1][3456789][0-9]{9}$/, "手机号码格式错误", dynamic, event);
};

//验证座机
agencyVerify.telephoneInput = function (form, selector, dynamic, event) {
    agencyVerify.regexCheck(form, selector, /^\d{3}-\d{8}|\d{4}-\d{7}$/, "座机号码格式错误", dynamic, event);
};

//邮箱验证
agencyVerify.checkEmail = function (form, selector, dynamic, event) {
    agencyVerify.regexCheck(form, selector, /^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/, "邮箱格式错误", dynamic, event);

};

//验证部门简称
agencyVerify.checkBrief = function (form, selector, dynamic, event) {
    agencyVerify.regexCheck(form, selector, /^([A-Za-z]|[\u4E00-\u9FA5])+$/, "部门简称错误", dynamic, event);

};

//非空
agencyVerify.notNullInput = function (form, selector, dynamic, event, excludeDefaultVal) {
    if (excludeDefaultVal) {
        excludeDefaultVal();
    }
    agencyVerify.regexCheck(form, selector, "", "此项必填", dynamic, event);
};

//正则表达式
agencyVerify.regexCheck = function (form, selector, patrn, errormsg, dynamic, event, runeventcondition) {
    if (!event) {
        event = "change";
    }
    var fun = function (e) {
        if (runeventcondition && typeof runeventcondition == "function") {
            if (!runeventcondition()) {
                return false;
            }
        }

        var temel = e.target || e;
        var temvaule = temel.value;
        var checkresult;
        if (patrn) {
            checkresult = (temvaule.match(patrn) || !temvaule);
        }
        else {
            //修改人 吴凯 ，处理输入空格的验证
            checkresult = (temvaule.replace(/\s/g, "") != "");
        }
        if (checkresult) {
            if (temvaule && patrn) {
                temel.value = temvaule.match(patrn)[0];
            }
            if ($(temel).attr("tabMsg")) {
                var wrongspan = $(temel).next().next();
                if (wrongspan.attr("class") == "wrong" && wrongspan.text() == errormsg) {
                    wrongspan.remove();
                    $(temel).removeClass("checkError");
                    $(temel).change();

                }
            }
            else {
                var wrongspan = $(temel).next();
                if (wrongspan.attr("class") == "wrong" && wrongspan.text() == errormsg) {
                    wrongspan.remove();
                    $(temel).removeClass("checkError");
                    //这一点很重要,当一个验证从不通过到通过时,需要重新验证所有在此元素上的验证事件
                    $(temel).change();
                }
            }

            return true;
        }
        else {
            $(temel).addClass("checkError");
            if ($(temel).attr("tabMsg")) {
                var wrongspan = $(temel).next().next();
                //如果文本框后面不是错误提示，或者是没有文字的错误提示（有些错误是自定义写在文本后面的）
                if (wrongspan.attr("class") != "wrong" || (wrongspan.attr("class") == "wrong" && wrongspan.text() == "")) {
                    var temspan = $(temel).next().after(agencyVerify.greatErrorHtml(temel, errormsg));

                }
            }
            else {
                var wrongspan = $(temel).next();
                if (wrongspan.attr("class") != "wrong" || (wrongspan.attr("class") == "wrong" && wrongspan.text() == "")) {
                    var temspan = $(temel).after(agencyVerify.greatErrorHtml(temel, errormsg));
                } else if ($(temel).attr("hasValue")) {
                    var temspan = $(temel).next().after(agencyVerify.greatErrorHtml(temel, errormsg));
                    $(temel).next().remove();
                    $(temel).removeAttr("hasValue");
                }
            }
            return false;
        }
    }

    var formSubmit = function () {
        var result = "";
        $(selector).each(function (i, el) {
            if (fun(el)) {
                result += "1";
            }
            else {
                result += "0";
            }
        });
        if ($(form).find(".wrong:first")) {
            var tempSelect = $(form).find(".wrong:first").not("[type=sameCheckAjax]");
            if (tempSelect.length > 0) {
                for (var i = 0; i < 1; i++) {
                    var temele = tempSelect.prev()[0];
                    if (temele.tagName == "INPUT" || temele.tagName == "TEXTAREA" || temele.tagName == "SELECT" || temele.tagName == "LABEL") {
                        //if (temele.getAttribute("mobile") && temele.getAttribute("mobile") == "Mobile") {
                        //    $(temele).attr("readonly", "readonly");
                        //    if ($(temele).attr("write")) {
                        //        $(temele).removeAttr("readonly");
                        //    }
                        //} else {
                            $(temele).focus();
                        //}
                        break;
                    }
                    temele = tempSelect.prev().prev()[0];
                    if (temele.tagName == "INPUT" || temele.tagName == "TEXTAREA" || temele.tagName == "SELECT") {
                        //if (temele.getAttribute("mobile") && temele.getAttribute("mobile") == "Mobile") {
                        //    $(temele).attr("readonly", "readonly");
                        //} else {
                            $(temele).focus();
                        //}
                        break;
                    }
                    temele = tempSelect.prev().prev().prev()[0];
                    if (temele.tagName == "INPUT" || temele.tagName == "TEXTAREA" || temele.tagName == "SELECT") {
                        //if (temele.getAttribute("mobile") && temele.getAttribute("mobile") == "Mobile") {
                        //    $(temele).attr("readonly", "readonly");
                        //} else {
                            $(temele).focus();
                        //}
                        break;
                    }
                }
            }


        }

        return result.indexOf("0") == -1;
    }
    if (form) {
        $(form).bind("submit", formSubmit);
    }

    if (dynamic) {
        if (form) {
            $(form).on(event, selector, fun);
        }
        else {
            $(selector).live(event, fun);
        }
    }
    else {
        $(selector).bind(event, fun);
    }
}
//生成用于提示错误的html,el表示文本框或其它可能会报错的元素
agencyVerify.greatErrorHtml = function (el, errormsg, customerStr) {
    var temel = $(el);
    var width = $(el).attr("alertWidth");
    var temtop, temleft;
    if (el.tagName == "TEXTAREA") {
        temtop = -88;
    }
    else {
        temtop = -30;
    }
    if (width) {
        temleft = width;
    }
    else {
        temleft = 0 - temel.outerWidth();
    }
    if (!customerStr) {
        customerStr = "";
    }
    return "<span " + customerStr + " class='wrong'><span  style='top:" + temtop + "px;left:" + temleft + "px' >" + errormsg + "<i></i></span></span>";
}
//多选下拉框非空验证
agencyVerify.notNullMultipleCheckBox = function (selector, id) {
    var len = $('#' + id + ' :checkbox[checked]').length;
    var wrong = $(selector).next('span.wrong')[0];
    if (len <= 0) {
        if (!wrong) {
            $(selector).addClass('checkError');
            //$(selector).after('<span class="wrong">该值不能为空</span>');
            $(selector).after(agencyVerify.greatErrorHtml($(selector), '此项必填'));
        }
        return false;
    } else {
        if (wrong) {
            $(selector).removeClass('checkError');
            $(wrong).remove();
        }
        return true;
    }
}

agencyVerify.getTxt1CursorPosition = function (e) {
    var oTxt1 = e;
    var cursurPosition = -1;
    if (oTxt1.selectionStart != undefined) {//非IE浏览器
        cursurPosition = oTxt1.selectionStart;
    } else {//IE
        var range = document.selection.createRange();
        //这个功能相当于在光标位置向左划鼠标选中文字,这里面的移动位数超了也无所谓,然后再计算选中文字的数量,就得到了光标的位置
        range.moveStart("character", -oTxt1.value.length);
        cursurPosition = range.text.length;
    }
    return cursurPosition;
}

agencyVerify.setCursor = function (elem, pos) {
    //elem.focus();  
    if (elem.setSelectionRange) { //W3C  
        elem.setSelectionRange(pos, pos);
    }
    else if (elem.createTextRange) { //IE  
        var range = elem.createTextRange(); //新建textRange对象    
        range.moveStart('character', pos); //更改range对象的开始位置  
        range.collapse(true); //光标移动到范围结尾  
        range.select(); //选中  
    }
}

//下拉菜单没选验证
agencyVerify.notdefaultSelect = function (form, selector, defaultValue, msg, dynamic) {
    var temfun = function (e) {
        var select = $(e.target || e);
        if (select[0].tagName != "SELECT") {
            select = $(selector);
        }
        var re = 0;
        for (var i = 0; i < select.length; i++) {
            var temselect = $(select[i]);
            var wrongspan = temselect.next();
            if (temselect.val() == defaultValue) {
                if (wrongspan.attr("class") != "wrong") {
                    temselect.after(agencyVerify.greatErrorHtml(temselect, msg));
                }
                re++;
            }
            else {
                if (wrongspan.attr("class") == "wrong" && wrongspan.text() == msg) {
                    wrongspan.remove();
                }
            }
        }
        return re == 0;
    }
    if (dynamic) {
        $(form).on("change", selector, temfun);
    }
    else {
        $(selector).bind("change", temfun);
    }
    $(form).bind("submit", temfun);
}

//单选框没选验证
agencyVerify.notCheckedRadio = function (form, selector, msg, dynamic) {
    var temfun = function () {
        var select = $(selector);
        var wrongspan = select.first().parent().next();
        if (!select.is(':checked')) {
            if (wrongspan.attr("class") != "wrong") {
                //select.last().parent().after("<span class='wrong'>" + msg + "</span>");
                select.first().parent().after(agencyVerify.greatErrorHtml(select.first().parent(), msg));
                select.bind("click", function () {
                    var wrongspan = select.first().parent().next();
                    if (wrongspan.attr("class") == "wrong" && wrongspan.text() == msg) {
                        wrongspan.remove();
                    }
                });
            }
            return false;
        }
        else {
            if (wrongspan.attr("class") == "wrong" && wrongspan.text() == msg) {
                wrongspan.remove();
            }
            return true;
        }
    }

    $(form).bind("submit", temfun);
}

//手机与座机二选一,房客源新增编辑专用,container在是指联系人的容器
agencyVerify.chooseMobileOrTel = function (formid, container) {
    var temfun = function () {
        var mobileOrTelCheckStr = "联系人手机电话至少填一个！";
        var checkedNumExist = $("#" + formid + " .checkedNumExist");
        var tembool = true;
        for (var i = 0; i < checkedNumExist.length; i++) {
            var mobile = $(checkedNumExist[i]).find("input[mobleFlag=mobleFlag]");
            var tel = $(checkedNumExist[i]).find("input[Tel=Tel]");
            var wrongspan = mobile.next();
            tembool = mobile.val() || tel.val();
            if (!tembool) {
                if (wrongspan.attr("tag") != "MoT") {
                    mobile.after(agencyVerify.greatErrorHtml(mobile, mobileOrTelCheckStr, "tag='MoT'"));
                }
            }
            else {
                if (wrongspan.attr("class") == "wrong" && wrongspan.text() == mobileOrTelCheckStr) {
                    wrongspan.remove();
                }
            }
        }
        if ($("#" + formid + "").find(".wrong[tag=MoT]").length > 0) {
            return false;
        }
        else {
            return true;
        }
    }
    $("#" + formid + "").bind("submit", temfun);
    $(container).on("keyup input paste", "input[mobleFlag=mobleFlag],input[Tel=Tel]", function () {
        var el = $(this);
        if (el.val() != "") {
            var temcontainer;
            if (el.attr("mobleFlag") == "mobleFlag") {
                temcontainer = el.parent();
            }
            else {
                temcontainer = el.parent().parent();
            }
            temcontainer.find(".wrong[tag=MoT]").remove();
        }
    });
}
agencyVerify.floatInput = function (selector, dynamic, form) {
    var fun = function (e) {

        var obj = $(e.target);
        var max = obj.attr("max");
        var min = obj.attr("min");
        var wrongspan = obj.next();
        var temvaule = e.target.value;
        var patrn = /^(-?[1-9]\d*\.\d*|-?0\.\d*|[1-9]\d*|-\d?|0)/;
        var point = agencyVerify.getTxt1CursorPosition(e.target);
        if (temvaule.match(patrn)) {
            e.target.value = e.target.value.match(patrn)[0];
            var rangeC = checkRange(temvaule, min, max);
            if (rangeC != null && $.trim(rangeC).length > 0) {
                if (wrongspan.attr("class") == "wrong") {
                    $(wrongspan).children("span").text(rangeC)
                } else {
                    obj.addClass("checkError");
                    obj.after(agencyVerify.greatErrorHtml(selector, rangeC));
                }

            } else {
                if (wrongspan.attr("class") == "wrong") {
                    wrongspan.remove();
                    obj.removeClass("checkError");
                }

            }
            if (e.type == "keyup") {
                agencyVerify.setCursor(e.target, point);
            }
        } else {
            if (wrongspan.attr("class") == "wrong" && $.inArray(wrongspan.text(), wrinings(temvaule, min, max)) >= 0) {
                wrongspan.remove();
                obj.removeClass("checkError");
            }
            e.target.value = "";
        }
    }
    var wrinings = function (val, min, max) {
        var arryWrn = ["请输入完整数值！"];
        arryWrn.push("值不能大于" + max);
        arryWrn.push("值不能小于" + min);
        return arryWrn;
    }
    var checkRange = function (val, min, max) {

        if (val != null && $.trim(val).length == 0) return;
        if (min > max) return;
        if (val == "-") {
            return "请输入完整数值！";
        }
        val = parseFloat(val);
        if (val < min) {
            return "值不能小于" + min;
        } else if (val > max) {
            return "值不能大于" + max;
        }

    }
    var formSubmit = function () {
        var obj = $(selector);
        for (var i = 0; i < obj.length; i++) {
            var temp = $(obj[i]);
            temp.blur();
            var wrongspan = temp.next();
            if (wrongspan.attr("class") == "wrong") {
                return false;
            }
        }
    }

    if (dynamic) {
        $(selector).live("keyup", fun);
        $(selector).live("blur", fun);
    } else {

        $(selector).bind("keyup", fun);
        $(selector).bind("blur", fun);
    }
    if (form) {
        $(form).bind("submit", formSubmit);
    }
};
//建筑面积与实用面积二选一，编辑新建房源专用
agencyVerify.chooseAreaOrRealArea = function () {
    var temfun = function () {
        var mobileOrTelCheckStr = "此项必填！";
        var Squaretxt = $('#Squaretxt');
        var SquareUse = $("#SquareUse");
        var wrongspan = Squaretxt.next();
        var tembool = true;
        tembool = Squaretxt.val() || SquareUse.val();
        if (!tembool) {
            if (wrongspan.attr("tag") != "MoT") {
                Squaretxt.after(agencyVerify.greatErrorHtml(Squaretxt, mobileOrTelCheckStr, "tag='MoT'"));
            }
        }
        else {
            if (wrongspan.attr("class") == "wrong" && wrongspan.text() == mobileOrTelCheckStr) {
                wrongspan.remove();
            }
        }
        if (tembool) {
            return true;
        }
        else {
            return false;
        }
    }
    $("#saveProperyEditForm").bind("submit", temfun);
    $("#Squaretxt,#SquareUse").bind("keyup input paste", function () {
        var el = $(this);
        if (el.val() != "") {
            $("#areaDiv").find(".wrong[tag=MoT]").remove();
        }
    });
}

//需要从智能提示中选择
agencyVerify.NeedSmartTips = function (form, selector, hidselector, msg, dynamic) {

    var temfun = function () {
        var select = $(selector);
        var wrongspan = select.next();
        if ($(hidselector).val() == "" && select.val() != "") {
            if (wrongspan.attr("class") != "wrong") {
                //select.after("<span class='wrong'>" + msg + "</span>");
                select.after(agencyVerify.greatErrorHtml(select, msg));
                select.addClass("checkError");
            }
            else {
                setTimeout(function () {
                    if ($(hidselector).val() != "" && select.val() != "") {
                        if (wrongspan.attr("class") == "wrong" && wrongspan.text() == msg) {
                            wrongspan.remove();
                            select.removeClass("checkError");
                        }
                    }
                }, 200);
            }
            return false;
        }
        else {
            if (wrongspan.attr("class") == "wrong" && wrongspan.text() == msg) {
                wrongspan.remove();
                select.removeClass("checkError");
                select.blur();
            }
            return true;
        }
    }
    if (dynamic) {
        if (form) {
            $(form).on("change", selector, function () { $(hidselector).val("") });
            $(form).on("blur", selector, temfun);
        }
        else {
            $(selector).live("change", function () { $(hidselector).val(""); });
            $(selector).live("blur", temfun);
        }
    }
    else {
        $(selector).bind("change", function () { $(hidselector).val(""); });
        $(selector).bind("blur", temfun);
    }
    $(form).bind("submit", temfun);

}


agencyVerify.CheckWdatePicker = function () {
    if ($(this).next().attr("class") == "wrong") {
        $(this).next().hide()
        $(this).removeClass("checkError");
    }
};



agencyVerify.NumberRange = function (form, selector, number, msg, event) {

    $(selector).unbind(event);
    //删除验证提醒信息
    var wrongspan = $(selector).next();
    var textMsg = wrongspan.get(0).innerText;
    if (wrongspan.attr("class") == "wrong" && textMsg == msg) {

        wrongspan.remove();
        $(selector).removeClass("checkError");
        //这一点很重要,当一个验证从不通过到通过时,需要重新验证所有在此元素上的验证事件
        //$(selector).change();
    }

    $(selector).bind(event, function () {
        var math = parseInt($(selector).val());
        var isHidden = $(selector).is(":visible");
        if (isHidden) {
            if (math < number) {
                $(selector).addClass("checkError");
                var wrongspan = $(selector).next();
                if (wrongspan.attr("class") != "wrong" ||
                    (wrongspan.attr("class") == "wrong" && wrongspan.text() == "")) {
                    var temspan = $(selector).after(agencyVerify.greatErrorHtml($(selector).get(0), msg));
                }
                return false;
            } else {
                var wrongspan = $(selector).next();
                var textMsg = wrongspan.get(0).innerText;
                if (wrongspan.attr("class") == "wrong" && textMsg == msg) {

                    wrongspan.remove();
                    $(selector).removeClass("checkError");
                    //这一点很重要,当一个验证从不通过到通过时,需要重新验证所有在此元素上的验证事件
                    $(selector).change();
                }
                return true;
            }
        }
    });


    var formSubmit = function () {
        var isHidden = $(selector).is(":visible");
        if (parseInt($(selector).val()) < number) {
            if (isHidden) {
                $(selector).focus();
                $(selector).change();
                return false;
            }
            return true;
        }
        else {
            return true;
        }
    }

    $(form).bind("submit", formSubmit);
}



//验证员工工号是否存在
agencyVerify.EmployeeNoExist = function (form, selector, msg, event) {    
    var object = $(selector);
    var wrongspan = $(selector).next();
    if (wrongspan.get(0)) {
        var textMsg = wrongspan.get(0).innerText;
        if (wrongspan.attr("class") == "wrong" && textMsg == msg) {
            wrongspan.remove();
            $(selector).removeClass("checkError");
        }
    }

    var isExist = false;
    $(selector).bind(event, function () {
        var wrongspan = $(selector).next();
        if (wrongspan.get(0)) {
            var textMsg = wrongspan.get(0).innerText;
            if (wrongspan.attr("class") == "wrong" && textMsg == msg) {
                wrongspan.remove();
                $(selector).removeClass("checkError");
                $(selector).blur();
            }
        }

        if (object.val() != "") {
            $.AjaxAgency('/Property/property-check-empno-ajax', { 'empno': object.val() }, function (data) {
                if (!data.Result) {
                    //$("#saveDM").attr("disabled", "disabled");
                    $(selector).addClass("checkError");
                    var wrongspan = $(selector).next();
                    if (wrongspan.attr("class") != "wrong" ||
                        (wrongspan.attr("class") == "wrong" && wrongspan.text() == "")) {
                        if (data.ErrorMsg != null) {
                            msg = data.ErrorMsg;
                        }
                        else {
                            msg = "员工工号不存在";
                        }
                        var temspan = $(selector).after(agencyVerify.greatErrorHtml($(selector).get(0), msg));
                    }
                    isExist = false;
                    return false;
                }
                else {
                    $("#saveDM").removeAttr("disabled");
                    isExist = true;
                    return true;
                }
            });
        }
    });

    var formSubmit = function () {
        if (!isExist) {
            $(selector).focus();
            $(selector).change();
            return false;
        }
        return true;
    }
    $(form).bind("submit", formSubmit);
}

//正则表达式验证员工证件号--ysw
agencyVerify.regexCheckIDCard = function (form, selector, errormsg, dynamic, event, runeventcondition, isEdit) {
    if (!event) {
        event = "change";
    }
    var fun = function (e) {
        if (runeventcondition && typeof runeventcondition == "function") {
            if (!runeventcondition()) {
                return false;
            }
        }
        var temel = e.target || e;
        var temvaule = temel.value;

        //if (isEdit && temvaule.length < 1) {
        //    var wrongspan = $(temel).next();
        //    wrongspan.remove();
        //    $(temel).removeClass("checkError");
        //    $(temel).change();
        //    return true;
        //}
        var checkresult;
        //if (temvaule.length == 18 && (temvaule.replace(/\s/g, "") != "")) {
        //    checkresult = (idCardNoUtil.check18ID(temvaule) || !temvaule);
        //} else if ((temvaule.length == 8 || temvaule.length == 9 || temvaule.length == 10) && (temvaule.replace(/\s/g, "") != "")) {
        //    checkresult = (idCardNoUtil.checkPassPort(temvaule) || !temvaule);
        //} else {
        //    checkresult = false;
        //}     checkID

        if (temvaule.replace(/\s/g, "") != "") {
            checkresult = (idCardNoUtil.checkID(temvaule) || !temvaule);
        } else {
            checkresult = false;
        }
        if (checkresult) {
            if ($(temel).attr("tabMsg")) {
                var wrongspan = $(temel).next().next();
                if (wrongspan.attr("class") == "wrong" && wrongspan.text() == errormsg) {

                    wrongspan.remove();
                    $(temel).removeClass("checkError");
                    $(temel).change();

                }
            }
            else {
                var wrongspan = $(temel).next();
                if (wrongspan.attr("class") == "wrong" && wrongspan.text() == errormsg) {
                    wrongspan.remove();
                    $(temel).removeClass("checkError");
                    //这一点很重要,当一个验证从不通过到通过时,需要重新验证所有在此元素上的验证事件
                    $(temel).change();
                } else if (wrongspan.attr("class") == "wrong" && (wrongspan.text().indexOf('该证件号已经存在A+中，无法创建') > -1)) {
                    wrongspan.remove();
                    $(temel).removeClass("checkError");
                    //这一点很重要,当一个验证从不通过到通过时,需要重新验证所有在此元素上的验证事件
                    $(temel).change();
                } else {
                    wrongspan.remove();
                    $(temel).removeClass("checkError");
                    //这一点很重要,当一个验证从不通过到通过时,需要重新验证所有在此元素上的验证事件
                    $(temel).change();
                }
                //else if (wrongspan.attr("class") == "wrong" && (wrongspan.text().indexOf('该身份证号已经存在CCHR中入职/离职审批中，无法创建') > -1)) {
                //    wrongspan.remove();
                //    $(temel).removeClass("checkError");
                //    //这一点很重要,当一个验证从不通过到通过时,需要重新验证所有在此元素上的验证事件
                //    $(temel).change();
                //} 
            }

            return true;
        }
        else {
            $(temel).addClass("checkError");
            if ($(temel).attr("tabMsg")) {
                var wrongspan = $(temel).next().next();
                //如果文本框后面不是错误提示，或者是没有文字的错误提示（有些错误是自定义写在文本后面的）
                if (wrongspan.attr("class") != "wrong" || (wrongspan.attr("class") == "wrong" && wrongspan.text() == "")) {
                    var temspan = $(temel).next().after(agencyVerify.greatErrorHtml(temel, errormsg));
                };
            }
            else {
                var wrongspan = $(temel).next();
                if (wrongspan.attr("class") != "wrong" || (wrongspan.attr("class") == "wrong" && wrongspan.text() == "")) {
                    var temspan = $(temel).after(agencyVerify.greatErrorHtml(temel, errormsg));
                } else if ($(temel).attr("hasValue")) {
                    var temspan = $(temel).next().after(agencyVerify.greatErrorHtml(temel, errormsg));
                    $(temel).next().remove();
                    $(temel).removeAttr("hasValue");
                } else if (wrongspan.attr("class") != "wrong" || (wrongspan.attr("class") == "wrong" && wrongspan.text().indexOf('该证件号已经存在A+中，无法创建') > -1)) {
                    var temspan = $(temel).next().after(agencyVerify.greatErrorHtml(temel, errormsg));
                    $(temel).next().remove();
                } else if (wrongspan.attr("class") != "wrong" || (wrongspan.attr("class") == "wrong" && wrongspan.text().indexOf('请正确录入身份证18位，护照8至10位') > -1)) {
                    var temspan = $(temel).next().after(agencyVerify.greatErrorHtml(temel, errormsg));//temvaule.replace(/\s/g, "").length>0?errormsg:'此项必填'));
                    $(temel).next().remove();
                } else {
                    var temspan = $(temel).next().after(agencyVerify.greatErrorHtml(temel, errormsg)); //temvaule.replace(/\s/g, "").length > 0 ? errormsg : '此项必填'));
                    $(temel).next().remove();
                }
                //else if (wrongspan.attr("class") != "wrong" || (wrongspan.attr("class") == "wrong" && wrongspan.text().indexOf('该身份证号已经存在CCHR中入职/离职审批中，无法创建') > -1)) {
                //    var temspan = $(temel).next().after(agencyVerify.greatErrorHtml(temel, errormsg)); //temvaule.replace(/\s/g, "").length > 0 ? errormsg : '此项必填'));
                //    $(temel).next().remove();
                //}                
            }
            return false;
        }
    };
    var formSubmit = function () {
        var result = "";
        $(selector).each(function (i, el) {
            if (fun(el)) {
                result += "1";
            }
            else {
                result += "0";
            }
        });
        if ($(form).find(".wrong:first")) {
            var tempSelect = $(form).find(".wrong:first").not("[type=sameCheckAjax]");
            if (tempSelect.length > 0) {
                for (var i = 0; i < 1; i++) {
                    var temele = tempSelect.prev()[0];
                    if (temele.tagName == "INPUT" || temele.tagName == "TEXTAREA" || temele.tagName == "SELECT" || temele.tagName == "LABEL") {
                        if (temele.getAttribute("mobile") && temele.getAttribute("mobile") == "Mobile") {
                            $(temele).attr("readonly", "readonly");
                            if ($(temele).attr("write")) {
                                $(temele).removeAttr("readonly");
                            }
                        } else {
                            $(temele).focus();
                        }
                        break;
                    }
                    temele = tempSelect.prev().prev()[0];
                    if (temele.tagName == "INPUT" || temele.tagName == "TEXTAREA" || temele.tagName == "SELECT") {
                        if (temele.getAttribute("mobile") && temele.getAttribute("mobile") == "Mobile") {
                            $(temele).attr("readonly", "readonly");
                        } else {
                            $(temele).focus();
                        }
                        break;
                    }
                    temele = tempSelect.prev().prev().prev()[0];
                    if (temele.tagName == "INPUT" || temele.tagName == "TEXTAREA" || temele.tagName == "SELECT") {
                        if (temele.getAttribute("mobile") && temele.getAttribute("mobile") == "Mobile") {
                            $(temele).attr("readonly", "readonly");
                        } else {
                            $(temele).focus();
                        }
                        break;
                    }
                }
            }


        }

        return result.indexOf("0") == -1;
    }
    if (form) {
        $(form).bind("submit", formSubmit);
    }

    if (dynamic) {
        if (form) {
            $(form).on(event, selector, fun);
        }
        else {
            $(selector).live(event, fun);
        }
    }
    else {
        $(selector).bind(event, fun);
    }
};

var idCardNoUtil = {
    checkID: function GetCheckIDcard(IDCard) {
        if (IDCard.length == 9) {
            var Passport = this.checkPassport(IDCard);// GetCheckPassport(IDCard); //验证护照格式
            //if (Passport == false) return false;
            return Passport;
        } else if (IDCard.length == 10) {
            var HKIDcard = this.checkHKIDCard(IDCard); //验证香港身份证格式
            if (!HKIDcard) {
                HKIDcard = this.checkAoMenIDCard(IDCard);
            }
            if (!HKIDcard) {
                HKIDcard = this.checkTaiWanIDCard(IDCard);//验证台湾身份证号
            }
            return HKIDcard;
        } else {
            //TxtIDCard, TxtDateBirth, TxtNativePlace, TxtSex, TxtAge
            //身份证的地区代码对照  
            var aCity = { 11: "北京", 12: "天津", 13: "河北", 14: "山西", 15: "内蒙古", 21: "辽宁", 22: "吉林", 23: "黑龙江", 31: "上海", 32: "江苏", 33: "浙江", 34: "安徽", 35: "福建", 36: "江西", 37: "山东", 41: "河南", 42: "湖北", 43: "湖南", 44: "广东", 45: "广西", 46: "海南", 50: "重庆", 51: "四川", 52: "贵州", 53: "云南", 54: "西藏", 61: "陕西", 62: "甘肃", 63: "青海", 64: "宁夏", 65: "新疆", 71: "台湾", 81: "香港", 82: "澳门", 91: "国外" };

            //合法性验证  
            var iSum = 0;
            //出生日期  
            var birthday;
            //验证长度与格式规范性的正则  
            var patten = new RegExp(/(^\d{15}$)|(^\d{17}(\d|x|X)$)/i);
            if (patten.exec(IDCard)) {
                //验证身份证的合法性的正则  
                patten = new RegExp(/^[1-9]\d{7}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])\d{3}$/);
                if (patten.exec(IDCard)) {
                    //获取15位证件号中的出生日期并转位正常日期                   
                    birthday = "19" + IDCard.substring(6, 8) + "-" + IDCard.substring(8, 10) + "-" + IDCard.substring(10, 12);

                }
                else {
                    //获取18位证件号中的出生日期  
                    birthday = IDCard.substring(6, 10) + "-" + IDCard.substring(10, 12) + "-" + IDCard.substring(12, 14);
                    //校验18位身份证号码的合法性  
                    IDCard = IDCard.replace(/x$/i, "a")//把身份证号码中的X转换成A
                    for (var i = 17; i >= 0; i--) iSum += (Math.pow(2, i) % 11) * parseInt(IDCard.charAt(17 - i), 11);
                    if (iSum % 11 != 1) {
                        //alert("你输入的证件号格式不正确，请检查！");
                        //$(selector).focus();
                        return false;
                    }
                }
                //检测证件地区的合法性  
                IDCard = IDCard.replace(/x$/i, "a");
                if (aCity[parseInt(IDCard.substring(0, 2))] == null) {
                    //alert("身份证地区非法,请检查！");
                    //$(selector).focus();
                    return false;
                }
                //给籍贯控件赋值
                //if (TxtNativePlace != "" && TxtNativePlace != null) {
                //    $("[id*=" + TxtNativePlace + "]").val(aCity[parseInt(IDCard.substr(0, 2))]);
                //}

                var dateStr = new Date(birthday.replace(/-/g, "/"));
                //为值添加0  
                function Append_zore(temp) {
                    if (temp < 10) {
                        return "0" + temp;
                    }
                    else {
                        return temp;
                    }
                }
                if (birthday != (dateStr.getFullYear() + "-" + Append_zore(dateStr.getMonth() + 1) + "-" + Append_zore(dateStr.getDate()))) {
                    //alert("身份证的出生日期非法，请检查");
                    //$(selector).focus();
                    return false;
                }
                //给出生日期赋值
                //if (TxtDateBirth != "" && TxtDateBirth != null) {
                //    $("[id*=" + TxtDateBirth + "]").val(birthday);
                //}
                ////给性别控件赋值
                //if (TxtSex != "" && TxtSex != null) {
                //    $("[id*=" + TxtSex + "]").val(IDCard.substr(16, 1) % 2 ? "男" : "女");
                //}
                ////给年龄控件赋值
                //if (TxtAge != "" && TxtAge != null) {
                //    $("[id*=" + TxtAge + "]").val(GetCheckBirthdayConvertAge_Year(birthday));
                //}
                return true;
            }
            else {
                //alert("输入的证件号码的长度或格式错误,请检查！");
                //$(selector).focus();
                return false;
            }
        }
    },

    //add ysw 2017-5-17 生日转年龄（按岁转的）
    checkBirthday: function GetCheckBirthdayConvertAge_Year(Birthday) {
        var Nowdate = new Date();
        Nowdate = Nowdate.format("yyyy-MM-dd");
        if (Birthday > Nowdate) {
            //alert("出生日期不能晚于当前日期，请输入正确的出生日期！"); //出生日期不能大于当前日期
            return false;
        }
        var age = 1;
        var Days = DateDiffDays(Nowdate, Birthday)
        if (Days > 43435) {
            //alert("请输入正确的出生日期,请检查!"); //不能大于119岁（43435）
            return false;
        }
        age = parseInt(parseFloat(Days) / 365);
        return age;
    },
    //add ysw 2017-5-17 验证护照是否正确
    checkPassport: function GetCheckPassport(obj) {
        var reg1 = /(^(P\.)|^(14)|^(15)|^(S\.)\d{7})$|(^[G|E|D|S]\d{8})$/;
        if (reg1.test(obj)) {
            return true;
        } else {
            //alert("你输入的证件号码格式不正确，请检查!");
            return false;
        }
    },

    /** 
    * add ysw 2017-5-17
    * 验证香港身份证 
    * @param obj -- HKUIDNumber 
    */
    checkHKIDCard: function GetCheckHKIDCard(obj) {
        var reg1 = /^[A-Z]{1}\d{6}\([0-9A]{1}\)$/; //香港格式1 (香港身份证号码结构：XYabcdef(z))  
        if (reg1.test(obj)) {
            return true;
        } else {
            //alert("输入的证件号码格式不正确，请检查！");
            return false;
        }
    },

    /** 
    * add qas 2018-8-14
    * 验证台湾身份证 
    * @param obj -- HKUIDNumber 
    */
    checkTaiWanIDCard: function GetCheckTaiWanIDCard(obj) {
        var reg1 = /^[A-Z][0-9]{9}$/; //台湾格式 (台湾身份证号码结构：A123456789 )  
        if (reg1.test(obj)) {
            return true;
        } else {
            return false;
        }
    },

    /** 
    * add qas 2018-8-14
    * 验证澳门身份证 
    * @param obj -- HKUIDNumber 
    */
    checkAoMenIDCard: function GetCheckAoMenIDCard(obj) {
        var reg1 = /^[157][0-9]{6}\([0-9]\)$/; //澳门格式 (澳门身份证号码结构：1123456(0) )  
        if (reg1.test(obj)) {
            return true;
        } else {
            return false;
        }
    },



};